notepad in console code
#include
#include
#include
#include
using namespace std;
void showMenu() {
cout << "\n--- Console Notepad ---\n";
cout << "1. New Note\n";
cout << "2. Load Note\n";
cout << "3. Save Note\n";
cout << "4. View Note\n";
cout << "5. Exit\n";
cout << "Choose an option: ";
}
void newNote(vector& buffer) {
buffer.clear();
cout << "Enter your note (type ':wq' to finish):\n";
string line;
while (getline(cin, line)) {
if (line == ":wq") break;
buffer.push_back(line);
}
}
void saveNote(const vector& buffer) {
string filename;
cout << "Enter filename to save: ";
cin >> filename;
cin.ignore(); // clear input buffer
ofstream file(filename);
if (file.is_open()) {
for (const string& line : buffer)
file << line << endl;
file.close();
cout << "Note saved to " << filename << endl;
} else {
cerr << "Error opening file for writing.\n";
}
}
void loadNote(vector& buffer) {
string filename;
cout << "Enter filename to load: ";
cin >> filename;
cin.ignore(); // clear input buffer
ifstream file(filename);
if (file.is_open()) {
buffer.clear();
string line;
while (getline(file, line))
buffer.push_back(line);
file.close();
cout << "Note loaded from " << filename << endl;
} else {
cerr << "File not found.\n";
}
}
void viewNote(const vector& buffer) {
cout << "\n--- Note Content ---\n";
for (const string& line : buffer)
cout << line << endl;
}
int main() {
vector noteBuffer;
int choice;
while (true) {
showMenu();
cin >> choice;
cin.ignore(); // clear newline from input
switch (choice) {
case 1:
newNote(noteBuffer);
break;
case 2:
loadNote(noteBuffer);
break;
case 3:
saveNote(noteBuffer);
break;
case 4:
viewNote(noteBuffer);
break;
case 5:
cout << "Exiting Notepad.\n";
return 0;
default:
cout << "Invalid option. Try again.\n";
}
}
return 0;
}
Code output
--- Console Notepad ---
1. New Note
2. Load Note
3. Save Note
4. View Note
5. Exit
Choose an option: 1
Enter your note (type ':wq' to finish):
This is my first note.
It has multiple lines.
:wq
--- Console Notepad ---
1. New Note
2. Load Note
3. Save Note
4. View Note
5. Exit
Choose an option: 4
--- Note Content ---
This is my first note.
It has multiple lines.
--- Console Notepad ---
1. New Note
2. Load Note
3. Save Note
4. View Note
5. Exit
Choose an option: 3
Enter filename to save: mynote.txt
Note saved to mynote.txt
--- Console Notepad ---
1. New Note
2. Load Note
3. Save Note
4. View Note
5. Exit
Choose an option: 5
Exiting Notepad.